package com.red.star.macalline.sched.service;

/**
 * @author AMGuo
 * @Description 蓄客卡
 * @date 2018/03/14 16:27
 */

import com.alibaba.fastjson.JSONObject;
import com.red.star.macalline.data.core.base.RedisRunner;
import com.red.star.macalline.data.mappers.StoredCardMapper;
import com.red.star.macalline.data.utils.CheckUtil;
import com.red.star.macalline.data.utils.HttpUtil;
import com.red.star.macalline.data.utils.TicketUtil;
import com.red.star.macalline.data.utils.WeiXinUtil;
import com.red.star.macalline.sched.core.constant.Constant;
import org.apache.http.client.fluent.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
@Transactional(readOnly = true)
public class StoredCardService {

    private static final Logger LOGGER = LoggerFactory.getLogger(StoredCardService.class);

    @Resource
    StoredCardMapper storedCardMapper;

    @Resource
    RedisRunner redisRunner;

    public static String MASS_SEND_TEMPLATE_ID;

    /**
     * 获取模板消息
     *
     * @param accessLogList
     * @param dayPage
     * @return
     */
    public List<Map> getTemplateMsg(List<String> accessLogList, String dayPage) throws IOException {
        List<Map> mapList = new ArrayList<>();
        for (String openId : accessLogList) {
            Map map = new HashMap();
            Map data = new HashMap();
            //发送给
            map.put(Constant.TEMPLATE_POST_TOUSER, openId);
            //template_id
            map.put(Constant.TEMPLATE_POST_TEMPLATE_ID, MASS_SEND_TEMPLATE_ID);
            //page
            map.put(Constant.TEMPLATE_POST_PAGE, dayPage);
            //form_id
            String formId = TicketUtil.getFormId(openId);
            map.put(Constant.TEMPLATE_POST_FORM_ID, formId);
            //内容1
            Map keyword1 = new HashMap();
            keyword1.put(Constant.TEMPLATE_POST_DATA_VALUE, "预约内容");
            keyword1.put(Constant.TEMPLATE_POST_DATA_COLOR, "#173177");
            Map keyword2 = new HashMap();
            keyword2.put(Constant.TEMPLATE_POST_DATA_VALUE, "活动描述");
            keyword2.put(Constant.TEMPLATE_POST_DATA_COLOR, "#173177");
            Map keyword3 = new HashMap();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            keyword3.put(Constant.TEMPLATE_POST_DATA_VALUE, sdf.format(new Date()));
            keyword3.put(Constant.TEMPLATE_POST_DATA_COLOR, "#173177");
            data.put(Constant.TEMPLATE_POST_DATA_1, keyword1);
            //内容2
            data.put(Constant.TEMPLATE_POST_DATA_2, keyword2);
            //内容3
            data.put(Constant.TEMPLATE_POST_DATA_3, keyword3);
            //data
            map.put(Constant.TEMPLATE_POST_DATA, data);
            mapList.add(map);
        }
        return mapList;
    }

    /**
     * 发送模板消息
     *
     * @param map
     */
    public void sendTemplateMsg(List<Map> map) throws IOException {
        for (Map paramMap : map) {
            String WXMessageUrl = WeiXinUtil.fecthWXMessageUrl(redisRunner);
            JSONObject body = fecthWXMessage(JSONObject.toJSONString(paramMap), WXMessageUrl);
            if (CheckUtil.isEmpty(body) || !Integer.valueOf(0).equals(body.getInteger("errcode"))) {
                LOGGER.warn("发送内容失败。openId：" + paramMap.get(Constant.TEMPLATE_POST_TOUSER));
            }
        }
    }

    /**
     * 推送模板消息请求
     *
     * @param data_b
     * @param WXMessageUrl
     * @return
     */
    private JSONObject fecthWXMessage(String data_b, String WXMessageUrl) throws IOException {
        Request request = Request.Post(WXMessageUrl);
        String body = HttpUtil.fetch(request, data_b);
        if (!CheckUtil.isEmpty(body)) {
            return JSONObject.parseObject(body);
        }
        return null;
    }
}
